package com.jointsky.jointframe.monitor.service;

import com.jointsky.jointframe.monitor.entity.AirRealTimeData;
import com.jointsky.jointframe.monitor.entity.AirRealTimeValue;
import com.jointsky.jointframe.monitor.repository.AirRealTimeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class AirRealTimeDataService {
    @Autowired
    private AirRealTimeRepository airRealTimeRepository;
    @Autowired
    private MongoTemplate mongoTemplate;

    public void save(AirRealTimeData airRealTimeData) {
        airRealTimeRepository.save(airRealTimeData);
    }

    public void update(AirRealTimeData airRealTimeData) {
        airRealTimeRepository.save(airRealTimeData);
    }

    public void deleteById(String id) {
        airRealTimeRepository.deleteById(id);
    }

    public List<AirRealTimeData> findAll() {
        List<AirRealTimeData> all = airRealTimeRepository.findAll();
        return all;
    }

    public AirRealTimeData findById(String id) {
        Optional<AirRealTimeData> optional = airRealTimeRepository.findById(id);
        return optional.get();
    }

    /**
     * 根据Id修改某个字段的值
     *
     * @param field
     * @param value
     * @param id
     */
    public void updateField(String field, Object value, String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = Update.update(field, value);
        mongoTemplate.upsert(query, update, AirRealTimeData.class);
    }

    /**
     * 更新内嵌文档字段属性
     *
     * @param field
     * @param oldValue
     * @param value
     * @param id
     */
    public void updateValuesField(String field, Object oldValue, Object value, String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id).and("values." + field).is(oldValue));
        Update update = new Update();
        update.set("values.$." + field, value);
        mongoTemplate.updateFirst(query, update, AirRealTimeData.class);
    }

    /**
     * 如果不存在则Push
     *
     * @param field
     * @param value
     * @param id
     */
    public void addToSet(String field, Object value, String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.addToSet(field, value);
        mongoTemplate.upsert(query, update, AirRealTimeData.class);
    }

    /**
     * 删除内嵌文档数据
     *
     * @param id
     * @param airRealTimeValue
     */
    public void deleteValue(String id, AirRealTimeValue airRealTimeValue) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.pull("values", airRealTimeValue);
        mongoTemplate.updateFirst(query, update, AirRealTimeData.class);
    }

    /**
     * 删除文档,根据id
     *
     * @param id
     */
    public void deleteDocument(String id) {
        Query query = Query.query(Criteria.where("id").is(id));
        mongoTemplate.remove(query, AirRealTimeData.class);
    }
}
